package com.koron.css2.serviceManage.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.PrintTemplateBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.PrintTemplateMapper;
import com.koron.css2.serviceManage.queryBean.PrintTempBusinessQueryBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.css2.serviceManage.vo.PrintTemplateBusiBeanVO;
import com.koron.util.Constant;
import com.koron.util.DBSourceUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 打印样式设计页面查询业务配置
 * @author admin
 *
 */
public class PrintTempBusinessList implements ServerInterface {

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {

		@SuppressWarnings("rawtypes")
		MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", PageInfo.class);

		try {
			PrintTempBusinessQueryBean bean = JsonUtils.objectToPojo(req.getData(), PrintTempBusinessQueryBean.class);
			PrintTemplateMapper mapper = factory.getMapper(PrintTemplateMapper.class);
			PageHelper.startPage(bean.getPage(), bean.getPageCount());
			//分页查询出打印业务配置
			List<PrintTemplateBusiBeanVO> list = mapper.listBusiness(bean);
			//查询出是否有打印样式设计
			//切换到水司数据源
			mapper = factory.getMapper(PrintTemplateMapper.class,DBSourceUtils.getDbEnv(bean.getCompanyNo()));
			List<PrintTemplateBusiBeanVO> newList = new ArrayList<>();
			for(PrintTemplateBusiBeanVO vo:list) {
				List<PrintTemplateBean> l =mapper.selectByBusinessId(vo.getBusiId());
				if(l !=null) {
					for(PrintTemplateBean tbean :l) {
						PrintTemplateBusiBeanVO newvo = new PrintTemplateBusiBeanVO();
						newvo.setBusiCode(vo.getBusiCode());
						newvo.setBusiId(vo.getBusiId());
						newvo.setBusiName(vo.getBusiName());
						newvo.setCreateName(tbean.getCreateName());
						newvo.setCreateTime(tbean.getCreateTime());
						newvo.setHasDesign(tbean.getHasDesign()==1?"已设计":"否");
						newvo.setTempId(tbean.getId());
						newvo.setTempName(tbean.getName());
						newvo.setTempRemark(tbean.getRemark());
						newvo.setUpdateName(tbean.getUpdateName());
						newvo.setUpdateTime(tbean.getUpdateTime());
						newList.add(newvo);
					}
				}
			}
			info.setData(new PageInfo<>(newList));
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription(e.getMessage());
			logger.error(e.getMessage(), e);
		}
		return info;
	}

}
